//
// Created by xili on 2024/8/30 9:41.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int n;

    int maxDistance(vector<int> &colors) {
        n = colors.size();
        int i = 0;
        for (; i < n; i++) {
            if (colors[i] != colors[0]) {
                break;
            }
        }
        return max(len(colors, 0), len(colors, i));
    }

    int len(vector<int> &colors, int i) {
        int cur = n - 1;
        while (cur >= i) {
            if (colors[cur] != colors[i]) {
                return cur - i;
            }
            cur--;
        }
        return 0;
    }
};